import CryptoJS from 'crypto-js'

/**
 * base64加密
 * @param {String} word
 */
export function encrypt (word) {
  const src = CryptoJS.enc.Utf8.parse(word)
  const key = CryptoJS.enc.Utf8.parse(process.env.VUE_APP_SECRET_KEY)
  // 加密模式为ECB，补码方式为PKCS5Padding（也就是PKCS7）
  const encrypted = CryptoJS.AES.encrypt(
    src,
    key,
    { mode: CryptoJS.mode.ECB, padding: CryptoJS.pad.Pkcs7 }
  )
  return encrypted.toString()
}

/**
 * base64 解密
 * @param {String} word
 */
export function decrypt (word) {
  const key = CryptoJS.enc.Utf8.parse(process.env.VUE_APP_LOGIN_SECRET_KEY)
  let decrypt = CryptoJS.AES.decrypt(
    word,
    key,
    { mode: CryptoJS.mode.ECB, padding: CryptoJS.pad.Pkcs7 }
  )
  decrypt = CryptoJS.enc.Utf8.stringify(decrypt)
  return decrypt.toString()
}
